package org.ala.tiktools.rpc.plugin.circuit_breaker;

import java.lang.reflect.Method;

import org.ala.tiktools.rpc.plugin.node.NodeKey;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 断路器
 *
 * @author ala
 * @date 2025-04-04 18:24
 */
public interface ICircuitBreaker extends ApplicationContextAware {
    /**
     *  空实现
     */
    public static final ICircuitBreaker NULL = new ICircuitBreaker() {
        @Override
        public void setApplicationContext(ApplicationContext context) throws BeansException {
        }
        @Override
        public boolean circuit(Method method, Object[] args, NodeKey key) {
            return false;
        }
        @Override
        public void fallback(Method method, Object[] args, NodeKey key) {
        }
        @Override
        public void fallbackNoActiveNode(Method method, Object[] args) {
        }
    };


    /**
     *  当前是否断路
     *  <p> 当没有有效通讯器，或者这里返回true时，执行断路
     */
    boolean circuit(Method method, Object[] args, NodeKey key);
    /**
     *  执行断路逻辑
     *  @param args 请求参数
     */
    void fallback(Method method, Object[] args, NodeKey key);
    /**
     *  执行断路逻辑
     *  @param args 请求参数
     */
    void fallbackNoActiveNode(Method method, Object[] args);
}
